package com.gbr.client.view;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

import com.gbr.application.model.Frame;
import com.gbr.client.presentation.IDesktopPresentation;

public class PlayerDelegate implements PropertyChangeListener {
    private IDesktopPresentation  presentation = null;
    private ImageView imageView = null;

    public PlayerDelegate(IDesktopPresentation prez) {
        presentation = prez;
        presentation.addPropertyChangeListener(this);
    }

    public Pane getPlayer() {
        BorderPane pane = new BorderPane();
        imageView = new ImageView();
        imageView.fitWidthProperty().bind(pane.widthProperty());
        imageView.setPreserveRatio(true);
        imageView.setSmooth(true);
        imageView.setCache(true);
        pane.setCenter(imageView);

        return pane;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        String property = evt.getPropertyName();
        if (property.equals(IDesktopPresentation.FRAME_SELECTION_CHANGED)) {
            final Frame newValue = (Frame) evt.getNewValue();
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    imageView.setImage(SwingFXUtils.toFXImage(newValue.getImage(), null));
                }
            });
        }
    }

}
